# -*- coding:utf-8 -*-
import pymongo
import sys
import mysqldb
import mongoDB

day = sys.argv[1]

newCar = "http%3A%2F%2Fhjk.m.autohome.com.cn%2Fws%2FInterim%2FIndex%3Fbid%3D0%26levels%3D%26price%3D0%2C0%26pvareaid%3D1001182"
newCarDb = "new_car_stat"
wc = "https%3A%2F%2Fdmarket.yongche.com%2Fwrapper%2Fmap%3Fkey%3D%25E5%258E%2595%25E6%2589%2580"
wcDb = "find_wc_stat"
oil = "car-master%3A%2F%2Fweiche_oil"
#oil = "https%3A%2F%2Fdmarket.yongche.com%2Fwrapper%2Fmap%3Fkey%3D%25E5%258A%25A0%25E6%25B2%25B9%25E7%25AB%2599"
oilDb = "find_oil_stat"
baoyang = "http%3A%2F%2Fapp.open.daojia.com%2Fdop%2Frouter%2Ftemplate%3FserviceType%3D1000072%26dopChannel%3D2%26cityid%3D1%26hmsr%3Dyidao"
baoyangDb = "baoyang_stat"
eDaijia = "http%3A%2F%2Fh5.edaijia.cn%2Fapp%2Findex.html%3Ffrom%3D01051399"
eDaijiaDb = "edaijia_stat"
#weizhang = "http%3A%2F%2Fyd.sebapi.top"
#weizhang = "http%3A%2F%2Fydh5.iseber.com"
weizhang = "car-master%3A%2F%2Fshibei_vio"
weizhangDb = "weizhang_stat"

collections = "dmarket_access_log"
collection = mongoDB.connMongo(collections)

ret = collection.find({"day": day})
try:
    for item in ret:
        if (item.has_key('u') and item.has_key('imei')):
#        if (item.has_key('u')) :
            url = item['u']
            #imei = ''
 #           if (item.has_key('imei')) :
            imei = item['imei']
            if ('' == imei):
                continue
            statTime = item['day']
            system = 0
            if item.has_key('system'):
                if ('android' == item['system'].lower()):
                    system = 1
                else:
                    system = 2
            if (url == newCar):
                mysqldb.inserData(newCarDb, imei, system, statTime)
            elif (url == wc):
                mysqldb.inserData(wcDb, imei, system, statTime)
            elif (url == oil):
                mysqldb.inserData(oilDb, imei, system, statTime)
            elif (url == baoyang):
                mysqldb.inserData(baoyangDb, imei, system, statTime)
            elif (url == eDaijia):
                mysqldb.inserData(eDaijiaDb, imei, system, statTime)
            elif (url == weizhang):
                mysqldb.inserData(weizhangDb, imei, system, statTime)
            else:
                continue
        else:
            continue
except Exception, e:
    print Exception, ":", e

